﻿using AutoMapper;
using ControlCenter.ApplicationService.Jobs;
using ControlCenter.ApplicationService.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MingYuanYun.Context;
using ControlCenter.Operation;
using Hangfire;

namespace ControlCenter.ApplicationService
{
    public static class ApplicationServiceCollectionExtensions
    {
        public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddOptions();

            services.AddSingleton(configuration);

            services.AddServiceContext();

            services.AddOperations(configuration);
            services.AddScoped<IPackageManagementService, PackageManagementService>();
            services.AddScoped<IRobotService, RobotService>();
            services.AddScoped<IResultCallbackService, ResultCallbackService>();
            services.AddScoped<IAuthService, AuthService>();
            services.AddScoped<IParamService, ParamService>();
            services.AddScoped<ILogService, LogService>();
            services.AddScoped<IFileService, FileService>();

            //注册任务
            services.AddScoped<IRobotStatusUpdateJob, RobotStatusUpdateJob>();
            services.AddAutoMapper();

            return services;
        }

        private static IServiceCollection AddAutoMapper(this IServiceCollection services)
        {
            Mapper.Initialize(cfg =>
            {
                cfg.AddProfile(new OperationAutoMapperProfile());
                cfg.AddProfile(new ApplicationAutoMapperProfile());
            });

            return services;
        }
    }
}
